package ee.nutilovi.praktikaportaal.company;

import java.io.IOException;

import javax.servlet.http.*;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Text;
import com.google.appengine.api.memcache.MemcacheService;
import com.google.appengine.api.memcache.MemcacheServiceFactory;

@SuppressWarnings("serial")
public class AddCompanyServlet extends HttpServlet {
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		String name = req.getParameter("name");
		String field = req.getParameter("field");
		Text description = new Text(req.getParameter("description"));

		Entity c = new Entity("Company");
		c.setProperty("name", name);
		c.setProperty("field", field);
		c.setProperty("description", description);
		c.setProperty("votes", 0);
		c.setProperty("score", 0);

		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		datastore.put(c);
		
		// Refresh stale cache.
		MemcacheService mc = MemcacheServiceFactory.getMemcacheService();
		mc.delete("companies");

		resp.sendRedirect("/companies");
	}
}
